package com.yesin.goods.listener;

import com.yesin.goods.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GoodsListener {

    @Autowired
    private GoodsHtmlService htmlService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "YESHOP.CREATE.WEB.QUEUE", durable = "true"),
            exchange = @Exchange(
                    value = "YESHOP.ITEM.EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}))
    public void listenCreate(Long id){
        if (id == null){
            return;
        }
        // 创建页面,会覆盖之前的页面
        this.htmlService.createHtml(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "YESHOP.DELETE.WEB.QUEUE", durable = "true"),
            exchange = @Exchange(
                    value = "YESHOP.ITEM.EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.delete"}))
    public void listenDelete(Long id){
        if (id == null){
            return;
        }
        this.htmlService.deleteHtml(id);
    }

}
